package com.rd.student.dashuju.zhengruixian.test0726;

public class TestSync implements Runnable {
    int ticketsNum = 100;

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            info();

    }

}

    private synchronized void info(){
        if (ticketsNum<=0){
            System.out.println("票卖光了");
            throw new RuntimeException();
        }
        System.out.println(Thread.currentThread().getName()+"出票"+ticketsNum);
        ticketsNum--;
    }

    public static void main(String[] args) {
        TestSync ticket=new TestSync();
        Thread t1=new Thread(ticket);
        Thread t2=new Thread(ticket);
        Thread t3=new Thread(ticket);
        t1.start();
        t2.start();
        t3.start();
    }
}



